DECLARE @xml XML

SET @xml = 
(
    SELECT
        SegmentId         = ISNULL(SegmentId,0)
    ,   DocumentId        = ISNULL(DocumentId,0)
    ,   Title             = CAST('<![CDATA[' + ISNULL(Title,'') + ']]>' AS xml)
    ,   DocumentShortName = CAST('<![CDATA[' + ISNULL(DocumentShortName,'') + ']]>' AS xml)
    ,   FileName          = CAST('<![CDATA[' + ISNULL(FileName,'') + ']]>' AS xml)
    ,   Path              = CAST('<![CDATA[' + ISNULL(Path,'') + ']]>' AS xml)
    ,   CreateDate        = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,CreateDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
    ,   ModificationDate  = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,ModificationDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
    ,   TemplateId        = ISNULL(CAST(TemplateId AS varchar(16)),'')
    ,   IsRoot            = ISNULL(IsRoot,0)
    ,   IsActive          = ISNULL(IsActive,0)
    ,   SortOrdinal       = ISNULL(CAST(SortOrdinal AS varchar(16)),'')
    ,   ClientId          = ISNULL(ClientId,'')
    ,   Tag               = CAST('<![CDATA[' + ISNULL(Tag,'') + ']]>' AS xml)
    FROM
        Document
    WHERE
        DocumentId = @DocumentId
    FOR XML AUTO, ELEMENTS
)

SELECT @xml
